home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / vopl / glvopl.lha / glvopl / gpp / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-16  |  4.4 KB  |  254 lines

  1. #include "gpp.h"
  2.  
  3. char    *fmttable[] = {
  4.     "",
  5.     "%f ",
  6.     "%f ",
  7.     "%f %f ",
  8.     "%f ",
  9.     "%f %f ",
  10.     "%f %f ",
  11.     "%f %f %f "
  12. };
  13.  
  14. extern    float    *newm1();
  15.  
  16. /*
  17.  * getline
  18.  *
  19.  *    reads in a file from file in.
  20.  */
  21. char *
  22. getline(in)
  23.     FILE    *in;
  24. {
  25.     char    *p, line[BUFSIZ]; 
  26.     int    c;
  27.  
  28.     p = line;
  29.  
  30.     while ((c = getc(in)) != '\n') {
  31.         if (feof(in))
  32.             return((char *)NULL);
  33.         *p++ = c;
  34.     }
  35.  
  36.     *p = 0;
  37.  
  38.     p = (char *)malloc(p - line + 1);
  39.     strcpy(p, line);
  40.  
  41.     return(p);
  42. }
  43.  
  44. /*
  45.  * decode
  46.  *
  47.  *    converts string type into its X, Y, and Z code.
  48.  */
  49. int
  50. decode(type)
  51.     char    *type;
  52. {
  53.     int    itype;
  54.     char    *sp;
  55.  
  56.     itype = 0;
  57.  
  58.     for (sp = type; *sp; sp++)
  59.         switch (*sp) {
  60.         case 'x':
  61.             itype |= X;
  62.             break;
  63.         case 'y':
  64.             itype |= Y;
  65.             break;
  66.         case 'z':
  67.             itype |= Z;
  68.             break;
  69.         default:
  70.             fprintf(stderr, "gpp: bad axes/type descriptor\n");
  71.             exit(1);
  72.         }
  73.  
  74.     return(itype);
  75. }
  76.  
  77. /*
  78.  * readgraphs
  79.  *
  80.  *    read in the graphs
  81.  */
  82. void
  83. readgraphs()
  84. {
  85.     int        type;
  86.     graph        *p, *lp;
  87.     char        stype[4], *fmt, *title;
  88.     int        atype, c;
  89.     float        x, y, z, *xaxis, *yaxis, *zaxis;
  90.  
  91.     ngraphs = 0;
  92.  
  93.     /*  First we read the title */
  94.  
  95.     if ((title = getline(infile)) == NULL) {
  96.         fprintf(stderr,"gpp: EOF when title was expected.\n");
  97.         exit(1);
  98.     }
  99.  
  100.     graphtitle(title);
  101.  
  102.     /* next the type */
  103.  
  104.     if (fscanf(infile, "type %s", stype) != 1) {
  105.         fprintf(stderr,"gpp: EOF when type expected.\n");
  106.         exit(1);
  107.     }
  108.  
  109.     getc(infile);        /* skip single newline */
  110.  
  111.     type = decode(stype);
  112.  
  113.     if ((type & X) && (xlabel = getline(infile)) == NULL) {
  114.         fprintf(stderr,"gpp: EOF when x-axis label expected.\n");
  115.         exit(1);
  116.     }
  117.     if ((type & Y) && (ylabel = getline(infile)) == NULL) {
  118.         fprintf(stderr,"gpp: EOF when y-axis label was expected.\n");
  119.         exit(1);
  120.     }
  121.     if ((type & Z) && (zlabel = getline(infile)) == NULL) {
  122.         fprintf(stderr,"gpp: EOF when z-axis label was expected.\n");
  123.         exit(1);
  124.     }
  125.  
  126.     /*  Now we should be reading coordinates */
  127.  
  128.     xaxis = (float *)NULL;
  129.     yaxis = (float *)NULL;
  130.     zaxis = (float *)NULL;
  131.  
  132.     p = gp = (graph *)malloc(sizeof(graph));
  133.     if (gp == (graph *)NULL) {
  134.         fprintf(stderr, "gpp: malloc returns NULL.\n");
  135.         exit(1);
  136.     }
  137.  
  138.     if ((gp->legend = getline(infile)) == NULL) {
  139.         fprintf(stderr,"gpp: EOF when legend was expected.\n");
  140.         exit(1);
  141.     }
  142.  
  143.     while (!feof(infile)) {
  144.         if (fscanf(infile, "axes %s ", stype) != 1) {
  145.             fprintf(stderr,"EOF when axes expected.\n");
  146.             exit(1);
  147.         }
  148.  
  149.         p->x = xaxis;
  150.         p->y = yaxis;
  151.         p->z = zaxis;
  152.  
  153.         atype = decode(stype);
  154.  
  155.         fmt = fmttable[atype];
  156.  
  157.         switch (atype) {
  158.         case X:
  159.             xaxis = p->x = newm1(MAXPNTS);
  160.             p->npnts = 0;
  161.             while (fscanf(infile, fmt, &x) != 0) {
  162.                 p->x[p->npnts++] = x;
  163.             }
  164.             break;
  165.         case Y:
  166.             yaxis = p->y = newm1(MAXPNTS);
  167.             p->npnts = 0;
  168.             while (fscanf(infile, fmt, &y) != 0) {
  169.                 p->y[p->npnts++] = y;
  170.             }
  171.             break;
  172.         case Z:
  173.             zaxis = p->z = newm1(MAXPNTS);
  174.             p->npnts = 0;
  175.             while (fscanf(infile, fmt, &z) != 0) {
  176.                 p->z[p->npnts++] = z;
  177.             }
  178.             break;
  179.         case X | Y:
  180.             xaxis = p->x = newm1(MAXPNTS);
  181.             yaxis = p->y = newm1(MAXPNTS);
  182.             p->npnts = 0;
  183.             while (fscanf(infile, fmt, &x, &y) != 0) {
  184.                 p->x[p->npnts] = x;
  185.                 p->y[p->npnts++] = y;
  186.             }
  187.             break;
  188.         case X | Z:
  189.             xaxis = p->x = newm1(MAXPNTS);
  190.             zaxis = p->z = newm1(MAXPNTS);
  191.             p->npnts = 0;
  192.             while (fscanf(infile, fmt, &x, &z) != 0) {
  193.                 p->x[p->npnts] = x;
  194.                 p->z[p->npnts++] = z;
  195.             }
  196.             break;
  197.         case Y | Z:
  198.             yaxis = p->y = newm1(MAXPNTS);
  199.             zaxis = p->z = newm1(MAXPNTS);
  200.             p->npnts = 0;
  201.             while (fscanf(infile, fmt, &y, &z) != 0) {
  202.                 p->y[p->npnts] = y;
  203.                 p->z[p->npnts++] = z;
  204.             }
  205.             break;
  206.         case X | Y | Z:
  207.             xaxis = p->x = newm1(MAXPNTS);
  208.             yaxis = p->y = newm1(MAXPNTS);
  209.             zaxis = p->z = newm1(MAXPNTS);
  210.             p->npnts = 0;
  211.             while (fscanf(infile, fmt, &x, &y, &z) != 0) {
  212.                 p->x[p->npnts] = x;
  213.                 p->y[p->npnts++] = y;
  214.             }
  215.             break;
  216.         default:
  217.             fprintf(stderr, "gpp: readgraphs - internal error.\n");
  218.             exit(1);
  219.         }
  220.  
  221.         if (fscanf(infile, "plot %s", stype) == 1) {
  222.             p->type = decode(stype);
  223.             if ((p->nxt = (graph *)malloc(sizeof(graph))) == (graph *)NULL) {
  224.                 fprintf(stderr, "readgraphs: malloc returns NULL\n");
  225.                 exit(1);
  226.             }
  227.             lp = p;
  228.             p = p->nxt;
  229.  
  230.             /*
  231.              * skip newline after plot command
  232.              */
  233.             while ((c = getc(infile)) != '\n' && !feof(infile))
  234.                 ;
  235.  
  236.             p->legend = getline(infile);
  237.  
  238.             ngraphs++;
  239.  
  240.             /*
  241.              * skip extra characters before next axis keyword
  242.              */
  243.             while ((c = getc(infile)) != 'a' && !feof(infile))
  244.                 ;
  245.  
  246.             if (!feof(infile))
  247.                 ungetc(c, infile);
  248.         }
  249.     }
  250.  
  251.     /* finish off the last one */
  252.     lp->nxt = NULL;
  253. }
  254.